package com.vast.admin.service;

import com.vast.admin.api.BestsignOpenApi;
import com.vast.admin.api.entity.ContractId;
import com.vast.admin.api.entity.CreateSignature;
import com.vast.admin.entity.Company;
import com.vast.admin.entity.ContractDetail;
import com.vast.admin.entity.bestsign.BestsignCallbackEntity;
import com.vast.admin.entity.bestsign.CertApplyCallback;
import com.vast.admin.entity.bestsign.SignContractCallback;
import com.vast.admin.service.impl.CompanyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class BestsignService {

    @Autowired
    private BestsignOpenApi bestsignOpenApi;

    @Autowired
    private ContractService contractService;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private BestsignCallbackService bestsignCallbackService;

    @Autowired
    private ContractDetailService contractDetailService;


    /**
     * 合同签署回调
     * {"action":"signContract","params":{"signerStatus":2,"code":"0","contractId":"157755309701000003","errMsg":"success","account":"5385cc955e4a4e3ebc548ab13e9e805b"}}
     * @param bestsignCallback
     */
    public void signContract(BestsignCallbackEntity bestsignCallback) {
        SignContractCallback signContractCallback = bestsignCallback.getParams().toJavaObject(SignContractCallback.class);
        try {
            if ("0".equals(signContractCallback.getCode()) && "success".equals(signContractCallback.getErrMsg())) {
                contractService.signSuccess(signContractCallback.getContractId(), signContractCallback.getAccount());
                Map<String, String> signerStatus = bestsignOpenApi.getSignerStatus(new ContractId(signContractCallback.getContractId())).getData();
                boolean flag = true;
                for (Map.Entry<String, String> entry : signerStatus.entrySet()) {
                    if (!"2".equals(entry.getValue())) {
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    //合同签署完成
                    List<ContractDetail> contractDetails = contractDetailService.getByBestsignContractId(signContractCallback.getContractId());
                    if (contractDetails != null && !contractDetails.isEmpty()) {
                        Long contractId = contractDetails.get(0).getContractId();
                        if (contractId != null) {
                            synchronized (contractId.toString()) {
                                contractService.completeContract(contractDetails);
                            }
                        }
                    }
                }
            }

        } finally {

            bestsignCallbackService.save(bestsignCallback);
        }
    }

    /**
     * 证书申请回调
     * {"action":"certApply","params":{"certType":"ZJCA","code":"0","cert":"ZJCA-11-20190809152522013-00001","message":"","account":"asdfasdfasd","taskId":"157755324801000002","status":"5"}}
     * @param bestsignCallback
     */
    public void certApply(BestsignCallbackEntity bestsignCallback) {
        CertApplyCallback certApplyCallback = bestsignCallback.getParams().toJavaObject(CertApplyCallback.class);
        try {
            if ("0".equals(certApplyCallback.getCode())) {
                Company company = companyService.getByBestsignAccount(certApplyCallback.getAccount(), certApplyCallback.getTaskId());
                if (company != null) {
                    //生成企业签章
                    CreateSignature createSignature = new CreateSignature();
                    createSignature.setAccount(company.getBestsignAccount());
                    createSignature.setText(company.getName());
                    log.info(createSignature.getAccount(), createSignature.getText());
                    bestsignOpenApi.createSignatureImage(createSignature);
                    Company updateEntity = new Company();
                    updateEntity.setId(company.getId());
                    updateEntity.setBestsignTokenStatus(Integer.parseInt(certApplyCallback.getStatus()));
                    updateEntity.setBestsignCertType(certApplyCallback.getCertType());
                    updateEntity.setBestsignCert(certApplyCallback.getCert());
                    companyService.updateById(updateEntity);
                }
            }

        } finally {
            bestsignCallbackService.save(bestsignCallback);
        }
    }
}
